/*
 * Copyright (C) 2011 Patrik 锟絢erfeldt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.puman.beautyfarmcounselor.ui.home.adpter;


import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Toast;

import com.puman.beautyfarmcounselor.MainActivity;
import com.puman.beautyfarmcounselor.R;
import com.puman.beautyfarmcounselor.ui.home.HomeFragment;
import com.puman.beautyfarmcounselor.ui.home.WebFragment;
import com.puman.beautyfarmcounselor.ui.home.image.SmartImageView;
import com.puman.beautyfarmcounselor.ui.home.model.AdertisingData;
import com.puman.beautyfarmcounselor.ui.home.model.Advertising;
import com.puman.beautyfarmcounselor.util.MethedUtil;

import java.util.List;

public class ImageAdapter extends BaseAdapter {
	//服务器图片URL
	private static final String ADV_0_URL = "http://img1.niutuku.com/hd/1308/18/18-niutuku.com-233968.jpg";
	private static final String ADV_1_URL = "http://116.228.60.178:9696/imagenotes/upload/image/2.jpg";
	private static final String ADV_2_URL = "http://pic13.nipic.com/20110312/420697_184526969000_2.jpg";
	private static final String ADV_3_URL = "http://116.228.60.178:9696/imagenotes/upload/image/3.jpg";
	private static final String ADV_4_URL = "http://pic13.nipic.com/20110317/6901455_132737346198_2.jpg";

	private HomeFragment homeFragment;
	private LayoutInflater mInflater;
	private static final String[] ids = {ADV_0_URL,ADV_1_URL, ADV_2_URL,ADV_3_URL, ADV_4_URL };
	private Advertising advertising;
	List<AdertisingData> adertisingDatas;

	public ImageAdapter(HomeFragment homeFragment, List<AdertisingData> adertisingDatas) {
		homeFragment = homeFragment;
		this.adertisingDatas = adertisingDatas;
//		for (int i = 0; i < adertisingDatas.size(); i++) {
//			MethedUtil.UtilLog("image adapter:" + adertisingDatas.get(i).getImgurl());
//		}
		mInflater = (LayoutInflater) homeFragment.getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}


	@Override
	public int getCount() {
		return Integer.MAX_VALUE;
	}
	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.image_item, null);
		}
		SmartImageView image = (SmartImageView) convertView
				.findViewById(R.id.imgView);
		String url = adertisingDatas.get(position % adertisingDatas.size()).getImgurl();
		image.setImageUrl(url, R.mipmap.ic_launcher, R.drawable.login_user);
//		image.setImageUrl(ids[position%ids.length], R.drawable.ic_launcher, R.drawable.login_user);
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				Intent intent = new Intent(mContext,AdvDetailActivity.class);
//				Bundle bundle = new Bundle();
//				bundle.putString("image_url", ids[position%ids.length]);
//				intent.putExtras(bundle);
//				mContext.startActivity(intent);
//				Toast.makeText(MainActivity.getInstace(), "position:" + (position + 1) % ids.length, Toast.LENGTH_SHORT).show();
//				if (homeFragment.isVisible()){
//					MainActivity.getInstace().switchFragment(homeFragment, new WebFragment());
//				}else{
//					Toast.makeText(MainActivity.getInstace(), "********" , Toast.LENGTH_SHORT).show();
//				}
//				Fragment fragment = new WebFragment_();
				MainActivity.getInstace().getSupportFragmentManager()
						.beginTransaction()
						.replace(R.id.content_none_bottom, new WebFragment(adertisingDatas.get(position % adertisingDatas.size()).getName(),adertisingDatas.get(position % adertisingDatas.size()).getWapurl())).addToBackStack(null).commit();


			}
		});
		return convertView;
	}

}
